﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMS.DB
{
    class CustomersMng : DBController
    {
        public List<Customer> GetAllItems()
        {
            var listItems = new List<Customer>();
            try
            {
                IQueryable<Customer> query = from item in DataEntities.Customers
                                             select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }
        public List<string> GetAllItemIds()
        {
            var listItemIds = new List<string>();
            try
            {
                IQueryable<string> query = from item in DataEntities.Customers
                                           select item.ID;

                listItemIds = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItemIds;
        }
        public List<Customer> GetItemByAllInfo(string info)
        {
            var allItem = new List<Customer>();
            try
            {
                allItem = GetAllItems();
                if (!string.IsNullOrEmpty(info))
                {
                    info = info.ToLower();
                    IQueryable<Customer> query = from item in DataEntities.Customers
                                                 where (item.ID.ToLower().Contains(info) ||
                                                 item.Name.ToLower().Contains(info) ||
                                                 item.PhoneNo.ToLower().Contains(info) ||
                                                 item.Address.ToLower().Contains(info) ||
                                                 item.Email.ToLower().Contains(info))
                                                 select item;
                    allItem = query.ToList();
                }
                return allItem;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return allItem;
        }

        public bool IsExist(Customer input)
        {
            if (input == null)
                return false;
            var itemId = input.ID;
            try
            {
                var output = DataEntities.Customers.Where(a => a.ID.ToLower() == itemId.ToLower());
                return output != null && output.Count() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return false;
        }
        public bool CheckVipCode(string vipcode)
        {
            if (vipcode == "")
                return false;
            var Item = new Customer();
            try
            {
                IQueryable<Customer> query = from item in DataEntities.Customers
                                             where item.VipCode.Equals(vipcode)
                                             select item;

                Item = query.FirstOrDefault();
                if (Item == null)
                    return false;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }

            return true;
        }
        public bool AddItem(string id, string name, string address, string phone, string email, decimal totalBuying, bool isVip, string vipCode)
        {
            try
            {
                var item = new Customer { ID = id,
                Name = name,
                Address = address,
                PhoneNo = phone,
                Email = email,
                TotalBuyingCost = totalBuying,
                NumberOfInvoice = 0,
                IsVip = isVip,
                VipCode = vipCode };
                if (IsExist(item))
                {
                    MessageBox.Show("Khách hàng này đã tồn tại.\nVui lòng kiểm tra lại mã NCC.");
                    return false;
                }
                DataEntities.AddToCustomers(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public Customer GetItemByElements(string id)
        {
            var Item = new Customer();
            try
            {
                IQueryable<Customer> query = from item in DataEntities.Customers
                                             where item.ID.Equals(id)
                                             select item;

                Item = query.FirstOrDefault();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return Item;
        }
        public Customer GetItemByDraftOrder(string ordercode)
        {
            var Item = new Customer();
            try
            {
                IQueryable<Customer> query = from item in DataEntities.Customers
                                             where item.Description.Equals(ordercode)
                                             select item;

                Item = query.FirstOrDefault();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return Item;
        }
        public Customer GetItemByVipCode(string code)
        {
            if (string.IsNullOrEmpty(code)) return null;

            var Item = new Customer();
            try
            {
                IQueryable<Customer> query = from item in DataEntities.Customers
                                             where item.VipCode.Equals(code)
                                             select item;

                Item = query.FirstOrDefault();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return Item;
        }

        public bool DeleteItem(string id)
        {
            try
            {
                var item = new Customer();
                item = GetItemByElements(id);
                if (item == null)
                    return false;
                DataEntities.DeleteObject(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool DeleteAll()
        {
            try
            {
                var list = GetAllItems();

                if (list != null && list.Count() > 0)
                {

                    foreach (Customer item in list)
                    {
                        DataEntities.DeleteObject(item);
                    }

                    DataEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }

            return true;

        }

        public string GetVipCode(string id)
        {
            try
            {
                var item = GetItemByElements(id);
                if (item != null)
                    return item.VipCode;
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return "";
            }
        }
        public bool UpdateItem(string Vipcode, decimal buyingCost)
        {
            try
            {
                var item = GetItemByVipCode(Vipcode);
                if (item == null)
                {
                    //MessageBox.Show(Constants.Msg.CustomerNotExist);
                    return false;
                }
                item.NumberOfInvoice += 1;
                item.TotalBuyingCost += buyingCost;

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public bool UpdateItem(string id, decimal buyingCost, string a)
        {
            try
            {
                var item = GetItemByElements(id);
                if (item == null)
                {
                    //MessageBox.Show(Constants.Msg.CustomerNotExist);
                    return false;
                }
                item.NumberOfInvoice += 1;
                item.TotalBuyingCost += buyingCost;

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool UpdateItem(string id, string draftOrderCode)
        {
            try
            {
                var item = GetItemByElements(id);
                if (item == null)
                {
                    MessageBox.Show(Constants.Msg.CustomerNotExist);
                    return false;
                }
                item.Description = draftOrderCode;

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }

        public bool UpdateItem(string id, string name, string phone, string address, string email, string desc, bool isVip, string vipCode)
        {
            try
            {
                var item = GetItemByElements(id);
                if (item == null)
                {
                    MessageBox.Show(Constants.Msg.CustomerNotExist);
                    return false;
                }
                item.Name = name;
                item.PhoneNo = phone;
                item.Address = address;
                item.Email = email;
                item.Description = desc;
                item.IsVip = isVip;
                item.VipCode = isVip ? vipCode : "";

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
    }
}
